/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.brokerage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.BaseController;
import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.services.manage.brokerage.BrokerageService;
import shop.services.manage.brokerage.bean.Brokerage;
import shop.services.manage.organize.OrganizeService;
import shop.services.manage.organize.bean.Organize;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;

/**   
 * @类名称：BrokerageAction      
 * @创建人：Ltz   
 * @创建时间：2017-08-12 上午10:16:03      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/brokerage/")
public class BrokerageAction extends BaseController<Brokerage> {
    private static final Logger logger = LoggerFactory.getLogger(BrokerageAction.class);
    @Autowired
    private BrokerageService brokerageService;
    @Autowired
    private OrganizeService organizeService;
    private static final String page_toList = "/manage/brokerage/brokerageList";
    public BrokerageService getService() {
        return brokerageService;
    }

    private BrokerageAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 查询总返利金额
     * @param brokerage
     * @return
     */
    @RequestMapping(value = "selectSum", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectSum(@ModelAttribute("brokerage") Brokerage brokerage) {
        int rs=0;
        try{
            rs =  brokerageService.selectSum(brokerage);
        }catch(Exception e){
            e.printStackTrace();
        }
        jsonResult = new JSONResult();
        jsonResult.setData(rs);
        return jsonResult;
    }

    /**
     * 查询总返利金额（机构）
     * @param brokerage
     * @return
     */
    @RequestMapping(value = "selectOrganizeSum", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrganizeSum(HttpServletRequest request,@ModelAttribute("brokerage") Brokerage brokerage) {
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        brokerage.setAffType("2");
        brokerage.setAffID(user.getId());
        int rs=0;
        try{
            rs =  brokerageService.selectSum(brokerage);
        }catch(Exception e){
            e.printStackTrace();
        }
        jsonResult = new JSONResult();
        jsonResult.setData(rs);
        return jsonResult;
    }

    /**
     * 查询Admin返利金额
     * @param brokerage
     * @return
     */
    @RequestMapping(value = "selectAdminSum", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectAdminSum(@ModelAttribute("brokerage") Brokerage brokerage) {
        int rs=0;
        try{
            rs =  brokerageService.selectAdminSum(brokerage);
        }catch(Exception e){
            e.printStackTrace();
        }
        jsonResult = new JSONResult();
        jsonResult.setData(rs);
        return jsonResult;
    }

    /**
     * 查询除admin以外返利金额总数
     * @param brokerage
     * @return
     */
    @RequestMapping(value = "selectNotAdminSum", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectNotAdminSum(@ModelAttribute("brokerage") Brokerage brokerage) {
        int rs=0;
        try{
            rs =  brokerageService.selectNotAdminSum(brokerage);
        }catch(Exception e){
            e.printStackTrace();
        }
        jsonResult = new JSONResult();
        jsonResult.setData(rs);
        return jsonResult;
    }


    /**
     * 查询机构账号返利明细
     *
     * @param request 请求
     * @param e       对象参数
     * @return 分页数据模型
     */
    @RequestMapping("loadOrganizeData")
    @ResponseBody
    public PagerModel loadOrganizeData(HttpServletRequest request, Brokerage e) {
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if (pageSize < 0) {
            pageSize = 10;
        }
        e.setOffset(offset);
        e.setPageSize(pageSize);
        User user = (User) request.getSession().getAttribute(ManageContainer.manage_session_user_info);
        Organize organize =new Organize();
        organize.setPrincipalPhone(user.getUsername());
        organize=organizeService.selectOne(organize);
        e.setAffType("2");
        e.setAffID(organize.getId());
        PagerModel pager = getService().selectPageList(e);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

}
